package com.webviewscrolldemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.webviewscrolldemo.view.VerticalSeekBar;
import com.webviewscrolldemo.view.VerticalSeekBar.OnVerticalSeekBarChangeListener;

public class MainActivity extends Activity {
	private final static String URL = "http://cloud-hk.mobile-kingdom.com/manual/ENGLISH/index.html";
	private final Activity activity = this;

	private WebView webView;
	private int allHeight = 100;
	private float webViewHeight = 0;
	//
	private VerticalSeekBar MyScrollview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.e("sss", "test");
		webView = (WebView) findViewById(R.id.webView);
		MyScrollview = (VerticalSeekBar) findViewById(R.id.MyScrollview);
		MyScrollview.setVisibility(View.GONE);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.setWebViewClient(new MyWebViewClient());
		
		webView.loadUrl(URL);
		
		
		webView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				if(action == event.ACTION_MOVE){
					Log.e("sss", "scaleX --> "+webView.getScaleX()+"  scaleY --> "+webView.getScaleY()+"  scrollX --> "+webView.getScrollX()+"  scrollY --> "+webView.getScrollY());
					
					int progress = (int) Math.ceil(MyScrollview.getMax() * (webViewHeight - webView.getScrollY()) / webViewHeight);
					MyScrollview.setLocation(progress);
				}
				
				return false;
			}
		});
		
		MyScrollview.setListener(new OnVerticalSeekBarChangeListener() {
			
			@Override
			public void onProgressChanged(VerticalSeekBar verticalSeekBar, int progress) {
				int scrollY = (int) (webViewHeight - progress * webViewHeight / MyScrollview.getMax());
				webView.setScrollY(scrollY);
			}
		});
	}
	
	private class MyWebViewClient extends WebViewClient{
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
		}
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			webViewHeight = webView.getContentHeight() * webView.getScale();
			Log.i("sss", "aaa --> "+webViewHeight);
			MyScrollview.setVisibility(View.VISIBLE);
		}
	}

}
